package com.avertymaroc.users;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.avertymaroc.app.R;
import com.avertymaroc.library.C;
import com.avertymaroc.library.HttpManager;
import com.avertymaroc.library.ImageLoader;
import com.avertymaroc.library.SessionManager;

public class Sondage_prive extends Activity {

	private ArrayList<HashMap<String, String>> list_sondage_priv;
	private ListView mListView;
	private MyAdapter mAdapter;
	private ImageButton logOut;
	private ImageButton homePage;
	private static final String KEY_TITRE 			= "titre";
	private static final String KEY_DESCRIPTION 	= "reponses";
	private static final String KEY_IMAGE 			= "image";
	protected static final String KEY_LINK 			= "link";
	protected static final String TAG_AUCCUN 		= "auccun";
	protected static final String TAG_SUCCESS 		= "success";
	protected static final String KEY_STATE 		= "state";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sondage_public);
		init();
	}

	private void init() {
		list_sondage_priv = new ArrayList<HashMap<String, String>>();
		mListView = (ListView) findViewById(R.id.list_sondage_pub);
		initActionBarButtons();
		
		
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View v,
					int position, long id) {
				HashMap<String, String> map = list_sondage_priv.get(position);
				Uri uri = Uri.parse( map.get(KEY_LINK ) );
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			}
		});

		HttpManager listSondage = new HttpManager(this) {

			@Override
			protected void onPostExecute(String file_url) {
				this.getpDialog().dismiss();

				JSONObject res = getResult();
				try {
					
				
					JSONArray sondagePrivee = res
							.getJSONArray("sondages_privees");
					String state = res.getString(KEY_STATE);
					if( state.equals(TAG_SUCCESS) )
					{
						HashMap<String, String> map;
	
						for (int i = 0; i < sondagePrivee.length(); i++) {
							map = new HashMap<String, String>();
							JSONObject obj = sondagePrivee.getJSONObject(i);
							map.put(KEY_TITRE, obj.getString("titre"));
							map.put(KEY_DESCRIPTION, obj.getString("description"));
							map.put(KEY_LINK, obj.getString("lien"));
							list_sondage_priv.add(map);
						}
					}
					mAdapter = new MyAdapter();

					if (list_sondage_priv.size() == 0) {
						TextView t = new TextView(getApplicationContext());
						t.setText("il n'y a pas de sondage actualement. ");
						mListView.addHeaderView(t);
					}

					mListView.setAdapter(mAdapter);

				} catch (JSONException e) {

					Toast.makeText(getApplicationContext(), e.getMessage(),
							3000).show();
					;
				}
			}
		};
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("user_id", new SessionManager(getApplicationContext())
				.getUserDetails().getId() + "");
		listSondage
				.setUrl(C
						.site_url("mobile/sondages_prives/get_liste_sondage_encours"))
				.setParams(params).execute();

	}

	class MyAdapter extends BaseAdapter {

		private LayoutInflater inflater = null;
		public ImageLoader imageLoader;

		public MyAdapter() {
			inflater = (LayoutInflater) Sondage_prive.this
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			imageLoader = new ImageLoader(getApplicationContext());
		}

		@Override
		public int getCount() {
			return list_sondage_priv.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null)
				convertView = inflater.inflate(R.layout.row_sondg_pub_list,
						null);

			HashMap<String, String> map = list_sondage_priv.get(position);

			TextView titre = (TextView) convertView
					.findViewById(R.id.titre_article);
			TextView nb_reponse = (TextView) convertView
					.findViewById(R.id.nb_reponse);
			// ImageView image = (ImageView)
			// convertView.findViewById(R.id.image_list);
			titre.setText(map.get(KEY_TITRE));
			nb_reponse.setText(map.get(KEY_DESCRIPTION));
			return convertView;
		}

	}
	
	private void initActionBarButtons(){
		 
		 logOut = (ImageButton) findViewById(R.id.ab_right_button);
		 homePage = (ImageButton) findViewById(R.id.ab_left_button);
		 
		 logOut.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new SessionManager(getApplicationContext()).logoutUser();
				
			}
		});
		 homePage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			
				finish();
				
			}
		});
		 
	}
}
